from orderSharing.retailOrderSharing import RetailOrderSharing


class AfterSales:

    def __init__(self):
        self.retailOrderSharing = RetailOrderSharing()

    def refund_of_hare(self, salesId, orderId):
        self.retailOrderSharing.session.commit()
        retail_profit_list = self.retailOrderSharing.session.execute(
            "select user_id, change_price from retail_user_frozen_price3_records where target_id = :targetId", params={"targetId": orderId}).fetchall()

        for retailProfit in retail_profit_list:
            self.retailOrderSharing.session.commit()
            refundProfit = self.retailOrderSharing.session.execute(
                "select change_price from retail_user_frozen_price3_records where user_id =: userId and target_id = :targetId",
                params={"userId":retailProfit[0], "targetId": salesId}).first()

            if refundProfit[0] != 0 - retailProfit[1]:
                print("{}还原零售金额错误，预期{}，实际{}".format(retailProfit[0], refundProfit[0], retailProfit[1]))

        self.retailOrderSharing.session.commit()
        profit_sharing_balance_list1 = self.retailOrderSharing.session.execute(
            "select user_id, change_price from retail_user_frozen_price4_records where target_id = :targetId",
            params={"targetId": orderId}).fetchall()

        for profitSharingBalance1 in profit_sharing_balance_list1:
            self.retailOrderSharing.session.commit()
            refundBalanceProfit1 = self.retailOrderSharing.session.execute(
                "select change_price from retail_user_frozen_price4_records where user_id =: userId and target_id = :targetId",
                params={"userId": profitSharingBalance1[0], "targetId": salesId}).first()

            if refundBalanceProfit1[0] != 0 - profitSharingBalance1[1]:
                print("{}还原分润差额1错误，预期{}，实际{}".format(profitSharingBalance1[0], refundBalanceProfit1[0], profitSharingBalance1[1]))

        self.retailOrderSharing.session.commit()
        profit_sharing_balance_list2 = self.retailOrderSharing.session.execute(
            "select user_id, change_price from retail_user_frozen_price5_records where target_id = :targetId",
            params={"targetId": orderId}).fetchall()

        for profitSharingBalance2 in profit_sharing_balance_list2:
            self.retailOrderSharing.session.commit()
            refundBalanceProfit2 = self.retailOrderSharing.session.execute(
                "select change_price from retail_user_frozen_price5_records where user_id =: userId and target_id = :targetId",
                params={"userId": profitSharingBalance2[0], "targetId": salesId}).first()

            if refundBalanceProfit2[0] != 0 - profitSharingBalance2[1]:
                print("{}还原分润差额2错误，预期{}，实际{}".format(profitSharingBalance2[0], refundBalanceProfit2[0],
                                                   profitSharingBalance2[1]))

        self.retailOrderSharing.session.commit()
        turnoverlist = self.retailOrderSharing.session.execute(
            "select user_id, change_price from retail_user_frozen_price1_records where target_id = :targetId",
            params={"targetId": orderId}).fetchall()

        for turnover in turnoverlist:
            self.retailOrderSharing.session.commit()
            refundTurnover = self.retailOrderSharing.session.execute(
                "select change_price from retail_user_frozen_price1_records where user_id =: userId and target_id = :targetId",
                params={"userId": turnover[0], "targetId": salesId}).first()

            if refundTurnover[0] != 0 - turnover[1]:
                print("{}还原营业额错误，预期{}，实际{}".format(turnover[0], refundTurnover[0],
                                                   turnover[1]))


